//
//  ScanerSettingView.swift
//  GreenDeal
//
//  Created by Moennig Q on 18/11/2022.
//

import UIKit

class ScanerSettingView: UIView {
    var fuzzySwitchBtnClosure: (()->Void)?
    var invertedSwitchBtnClosure: (()->Void)?
    
    let backView = UIView()
    
    let invertedSwitchBtn = SwitchButton()
    let fuzzySwitchBtn = SwitchButton()
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpView()
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setUpView() {
        //取消交互能力
        self.backgroundColor = .clear
        backView.drawCorner(byRoundingCorners: .allCorners)
        self.addSubview(backView)
        backView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.height.width.greaterThanOrEqualTo(0)
            make.bottom.equalTo(-100)
        }
        //1.fuzzyImageView
        let fuzzyImageView = UIImageView(image: UIImage(named: "scanner_setting_fuzzy"))
        backView.addSubview(fuzzyImageView)
        fuzzyImageView.snp.makeConstraints { make in
            make.left.top.equalTo(20)
            make.width.height.equalTo(24)
        }
        let fuzzyLabel = BaseLabel()
        fuzzyLabel.text = "Fuzzy Code Scanning"
        fuzzyLabel.setFont(R.font.sfProTextMedium(size: 15))
        fuzzyLabel.textColor = UIColor(hexStr: "#292D32")
        backView.addSubview(fuzzyLabel)
        fuzzyLabel.snp.makeConstraints { make in
            make.left.equalTo(fuzzyImageView.snp.right).offset(12)
            make.width.height.greaterThanOrEqualTo(0)
            make.centerY.equalTo(fuzzyImageView)
        }
        //
        
        backView.addSubview(fuzzySwitchBtn)
        fuzzySwitchBtn.snp.makeConstraints { make in
            make.left.equalTo(fuzzyLabel.snp.right).offset(60)
            make.right.equalTo(-20)
            make.centerY.equalTo(fuzzyImageView)
            make.size.equalTo(SwitchButton.size)
        }
        //2
        let invertedImageView = UIImageView(image: UIImage(named: "scanner_setting_invertedcode"))
        backView.addSubview(invertedImageView)
        invertedImageView.snp.makeConstraints { make in
            make.left.width.height.equalTo(fuzzyImageView)
            make.top.equalTo(fuzzyImageView.snp.bottom).offset(24)
            make.bottom.equalTo(-20)
        }
        let invertedLabel = BaseLabel()
        invertedLabel.text = "Scan Inverted Barcode"
        invertedLabel.setFont(R.font.sfProTextMedium(size: 15))
        invertedLabel.textColor = fuzzyLabel.textColor
        backView.addSubview(invertedLabel)
        invertedLabel.snp.makeConstraints { make in
            make.left.equalTo(fuzzyLabel)
            make.width.height.greaterThanOrEqualTo(0)
            make.centerY.equalTo(invertedImageView)
        }
        
        backView.addSubview(invertedSwitchBtn)
        invertedSwitchBtn.snp.makeConstraints { make in
            make.right.size.equalTo(-20)
            make.centerY.equalTo(invertedImageView)
            make.size.equalTo(fuzzySwitchBtn)
        }
        fuzzySwitchBtn.addTarget(self, action: #selector(fuzzySwitchBtnClick), for: UIControl.Event.touchUpInside)
        invertedSwitchBtn.addTarget(self, action: #selector(invertedSwitchBtnClick), for: UIControl.Event.touchUpInside)
    }
    @objc func invertedSwitchBtnClick(){
        self.invertedSwitchBtnClosure?()
    }
    @objc func fuzzySwitchBtnClick(){
        self.fuzzySwitchBtnClosure?()
    }
    func setFuzzySwitchBtn(_ onFlag: Bool){
        fuzzySwitchBtn.switchOn(onFlag)
    }
    
    func setInvertedSwitchBtn(_ onFlag: Bool){
        invertedSwitchBtn.switchOn(onFlag)
    }
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        for touch in touches{
            let point = touch.location(in: self)
            if !backView.frame.contains(point){//当前触摸点不在backview上时，才执行
                self.removeFromSuperview()
            }
        }
    }
}
